package com.ryan.demo.grpc.client.provider;

import com.ryan.demo.grpc.api.AckReceiptData;
import com.ryan.demo.grpc.api.ReceiveEventData;
import com.ryan.demo.grpc.api.SendAckData;
import com.ryan.demo.grpc.api.SubEventHubGrpc;
import com.ryan.demo.grpc.client.service.SendAckGrpcService;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;


/**
 * @author MRyan
 */
@Slf4j
@Service
public class SubEventHubGrpcServiceImpl extends SubEventHubGrpc.SubEventHubImplBase {

    @Resource
    private SendAckGrpcService sendAckGrpcService;

    @PostConstruct
    public void init() {
        assert sendAckGrpcService != null : "sendAckGrpcService is null";
    }

    @Override
    public void receiveEvent(ReceiveEventData receiveEventData, StreamObserver<AckReceiptData> responseObserver) {
        log.info("Start receiveEvent");

        String domain = receiveEventData.getDomain();
        String scene = receiveEventData.getScene();
        String command = receiveEventData.getCommand();
        String metaData = receiveEventData.getMetaData();

        AckReceiptData ackReceiptData = sendAckGrpcService.sendAck(SendAckData.newBuilder()
                .setDomain(domain)
                .setScene(scene)
                .setCommand(command)
                .setMetaData(metaData)
                .build());

        responseObserver.onNext(ackReceiptData);
        responseObserver.onCompleted();
    }
}
